<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="600px"
    :before-close="handleClose"
    label-width="auto"
    :close-on-click-modal="false">
    <el-form
      ref="quotaFormRef"
      :model="formData"
      :rules="formRules"
      label-width="auto">
      <el-form-item label="用户ID">
        {{ formData.customer_id }}
      </el-form-item>
      <el-form-item label="用户名称">
        <el-input class="w-280" v-model="formData.customer_name"></el-input>
      </el-form-item>
      <el-form-item label="权益配额">
        <el-form-item label="容器组数量" prop="pods">
          <el-input-number :min="0" :step="1" step-strictly :controls="false" class="w-280" v-model="formData.pods"></el-input-number>个
        </el-form-item>
        <el-form-item label="CPU核数" prop="cpu">
          <el-input-number :min="0" :step="1" step-strictly :controls="false" class="w-280" v-model="formData.cpu"></el-input-number>个
        </el-form-item>
        <el-form-item label="内存容量" prop="ram">
          <el-input-number :min="0" :step="1" step-strictly :controls="false" class="w-280" v-model="formData.ram"></el-input-number>GB
        </el-form-item>
        <el-form-item label="临时存储容量" prop="tmp_storage">
          <el-input-number :min="0" :step="1" step-strictly :controls="false" class="w-280" v-model="formData.tmp_storage"></el-input-number>GB
        </el-form-item>
        <el-form-item label="A5000数量" prop="a5000">
          <el-input-number :min="0" :step="1" step-strictly :controls="false" class="w-280" v-model="formData.a5000"></el-input-number>个
        </el-form-item>
        <el-form-item label="4090数量" prop="rtx4090">
          <el-input-number :min="0" :step="1" step-strictly :controls="false" class="w-280" v-model="formData.rtx4090"></el-input-number>个
        </el-form-item>
        <el-form-item label="3090数量" prop="rtx3090">
          <el-input-number :min="0" :step="1" step-strictly :controls="false" class="w-280" v-model="formData.rtx3090"></el-input-number>个
        </el-form-item>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { reactive, ref, computed, watch } from 'vue';
import quotaService from '@/service/eci/quota'
import { ElMessage } from 'element-plus'
export default{
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '提示'
    },
    type: {
      type: String,
      default: 'add'
    },
    curRow: {
      type: Object,
      default: () => {}
    }
  },
  emits: ['update:visible', 'searchList'],
  setup(props, {emit}) {
    const dialogVisible = computed({
      get() {
        return props.visible
      },
      set(val) {
        emit('update:visible', val)
      }
    })
    const handleClose = () => {
      dialogVisible.value = false
    }
    const formData = ref(null)
    watch(() => props.curRow, (nVal) => {
      if(nVal) {
        formData.value = Object.assign({}, props.curRow)
      }
    }, {deep: true, immediate: true})
    
    const formRules = {
      pods: [
        { required: true, message: '请输入容器组数量', trigger: ['blur', 'change'] }
      ],
      cpu: [
        { required: true, message: '请输入CPU核数', trigger: ['blur', 'change'] }
      ],
      ram: [
        { required: true, message: '请输入内存容量', trigger: ['blur', 'change'] }
      ],
      tmp_storage: [
        { required: true, message: '请输入临时存储容量', trigger: ['blur', 'change'] }
      ],
      a5000: [
        { required: true, message: '请输入A5000数量', trigger: ['blur', 'change'] }
      ],
      rtx4090: [
        { required: true, message: '请输入4090数量', trigger: ['blur', 'change']}
      ],
      rtx3090: [
        { required: true, message: '请输入3090数量', trigger: ['blur', 'change']}
      ]
    }
    const quotaFormRef = ref(null)
    const handleConfirm = () => {
      quotaFormRef.value.validate(valid => {
        if(valid) {
          const {customer_id, customer_name, available_zone_id, user_id, pods, cpu, ram, tmp_storage, a5000, rtx4090, rtx3090} = formData.value
          let params = {
            action: 'new',
            customer_id,
            customer_name,
            user_id,
            available_zone_id,
            set_data: {
              pods,
              cpu,
              ram,
              tmp_storage,
              a5000,
              rtx4090,
              rtx3090
            }
          }
          quotaService.setQuota(params).then(res => {
            if(res.code === 'Success') {
              ElMessage.success("保存成功")
              dialogVisible.value = false
              emit('searchList')
              handleClose()
            }
          })
        }
      })
    }
    return {
      dialogVisible,
      handleClose,
      formRules,
      formData,
      handleConfirm,
      quotaFormRef,
    }
  }
}


</script>
<style scoped>

</style>